package com.demo.bio.five;

import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.UUID;

/**
 * @author wangning
 * @create 2022-01-05 21:56
 */
public class ServerReaderThread extends Thread {
	private Socket socket;

	public ServerReaderThread(Socket socket) {
		this.socket = socket;
	}
	@Override
	public void run() {
		try {
			//1.得到一个数据输入流读取客户端发送过来的数据
			DataInputStream dis = new DataInputStream(socket.getInputStream());
			//2.读取客户端发送来的文件类型
			String suffix = dis.readUTF();
			System.out.println("服务端接收到的文件类型" + suffix);

			//3.定义一个字节输出管道，负责把客户端发送来的文件数据写出去
			//C:\Users\acer\Desktop\BIO-NIO-AIO\server
			OutputStream os = new FileOutputStream("C:\\Users\\acer\\Desktop\\BIO-NIO-AIO\\server\\" +
					UUID.randomUUID().toString() + suffix);
			//4.从数据输入流中读取文件数据，然后写入文件输出流去
			byte[] buffer = new byte[1024];
			int len;
			while ((len = dis.read(buffer)) > 0) {
				os.write(buffer, 0, len);
			}
			os.close();
			System.out.println("服务端接收文件，保存成功！");


		} catch (Exception e) {
			e.printStackTrace();

		}

	}
}
